package com.cpecc.controller.wuzi;

import java.util.Date;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;
import com.cpecc.mail.SendMailThread;
import com.cpecc.manager.system.EmailManager;
import com.cpecc.manager.system.HuanJieManager;
import com.cpecc.manager.system.RootManager;
import com.cpecc.manager.system.ShiYongZTManager;
import com.cpecc.manager.system.ToDoManager;
import com.cpecc.manager.system.UserManager;
import com.cpecc.manager.system.YouJianManager;
import com.cpecc.manager.wuzi.HuanHuoManager;
import com.cpecc.manager.wuzi.ShenQingJLManager;
import com.cpecc.manager.wuzi.ShenQingManager;
import com.cpecc.manager.wuzi.TuiHuoJLManager;
import com.cpecc.manager.wuzi.TuiHuoManager;
import com.cpecc.manager.wuzi.WeiXiuManager;
import com.cpecc.manager.wuzi.WuZiManager;
import com.cpecc.model.system.Email;
import com.cpecc.model.system.ToDo;
import com.cpecc.model.system.User;
import com.cpecc.model.system.YouJian;
import com.cpecc.model.wuzi.ShenQing;
import com.cpecc.model.wuzi.ShenQingJL;
import com.cpecc.model.wuzi.TuiHuo;
import com.cpecc.model.wuzi.TuiHuoJL;
import com.cpecc.model.wuzi.WuZi;

@Controller
@RequestMapping(value = "/wuzi")
public class TuiHuoController {
	
	@Autowired
	private UserManager userManager;
	
	@Autowired
	private ShenQingManager shenQingManager;
	
	@Autowired
	private HuanJieManager huanJieManager;
	
	@Autowired
	private WeiXiuManager weiXiuManager;
	
	@Autowired
	private HuanHuoManager huanHuoManager;
	
	@Autowired
	private TuiHuoManager tuiHuoManager;
	
	@Autowired
	private TuiHuoJLManager tuiHuoJLManager;
	
	@Autowired
	private ShiYongZTManager shiYongZTManager;
	
	@Autowired
	private ShenQingJLManager shenQingJLManager;
	
	@Autowired
	private WuZiManager wuZiManager;
	
	@Autowired
	private ToDoManager toDoManager;
	
	@Autowired
	private YouJianManager youJianManager;
	
	@Autowired
	private RootManager rootManager;
	
	@Autowired
	private EmailManager emailManager;

	/*
	 * 申请退货
	 */
	
	@RequestMapping(value = "/cgthList")
	public ModelAndView cgthList(){
		ModelAndView mav=new ModelAndView();
		Subject currentUser = SecurityUtils.getSubject();
		Object obj=currentUser.getPrincipals();
		String name=obj.toString();
		User user=userManager.findByUsername(name);
		mav.addObject("tuiHuoCGList", tuiHuoManager.getAllTuiHuoSQ2CMR(302,user));
		mav.setViewName("shenqing/cgthListAjax");
		return mav;
	}
	
	@RequestMapping(value = "/tuiHuo")
	public ModelAndView tuiHuo(String id){
		ModelAndView mav=new ModelAndView();
		mav.addObject("shenQingId", id);
		mav.setViewName("shenqing/tuiHuo");
		return mav;
	}
	
	@RequestMapping(value = "/tuiHuoShenQing")
	public void huanHuoShenQing(String shenQingId,String tuiHuoYY,HttpServletResponse response){
		Subject currentUser = SecurityUtils.getSubject();
		Object obj=currentUser.getPrincipals();
		String name=obj.toString();
		User user=userManager.findByUsername(name);
		ShenQing shenQing=shenQingManager.get(shenQingId);
		int dangQianHJ=shenQing.getHuanJie().getId();
		shenQing.setHuanJie(huanJieManager.get(302));
		shenQingManager.cover(shenQing);
		WuZi wuZi=shenQing.getWuZi();
		String wuZiId=wuZi.getId();
		if(weiXiuManager.hasWuZi(wuZiId)){
			response.setContentType("application/json");
			String result="{\"result\":\"conflict\"}";
			try {
				response.getWriter().write(result);
			} catch (Exception e) {
				e.printStackTrace();
			}
			return;
		}else if(huanHuoManager.hasWuZi(wuZiId)){
			response.setContentType("application/json");
			String result="{\"result\":\"conflict\"}";
			try {
				response.getWriter().write(result);
			} catch (Exception e) {
				e.printStackTrace();
			}
			return;
		}else if(tuiHuoManager.hasWuZi(wuZiId)){
			response.setContentType("application/json");
			String result="{\"result\":\"conflict\"}";
			try {
				response.getWriter().write(result);
			} catch (Exception e) {
				e.printStackTrace();
			}
			return;
		}		
		wuZi.setShiYongZT(shiYongZTManager.get(113));
		wuZiManager.cover(wuZi);
		ShenQingJL shenQingJL=new ShenQingJL();
		shenQingJL.setCaoZuoRQ(new Date());
		shenQingJL.setHuanJie(huanJieManager.get(dangQianHJ));
		shenQingJL.setPiFuLX("采购退货");
		shenQingJL.setPiFuNR("申请人申请退货");
		shenQingJL.setShenQing(shenQing);
		shenQingJL.setUser(user);
		shenQingJLManager.save(shenQingJL);
		TuiHuo tuiHuo=new TuiHuo();
		tuiHuo.setHuanJie(huanJieManager.get(302));
		tuiHuo.setShenQing(shenQing);
		tuiHuo.setShenQingRQ(new Date());
		tuiHuo.setTuiHuoYY(tuiHuoYY);
		tuiHuo.setUser(user);
		tuiHuo.setWuZi(shenQing.getWuZi());
		tuiHuoManager.save(tuiHuo);
		TuiHuoJL tuiHuoJL=new TuiHuoJL();
		tuiHuoJL.setCaoZuoRQ(new Date());
		tuiHuoJL.setHuanJie(huanJieManager.get(301));
		tuiHuoJL.setPiFuLX("申请退货");
		tuiHuoJL.setPiFuNR("申请人申请退货");
		tuiHuoJL.setTuiHuo(tuiHuo);
		tuiHuoJL.setUser(user);
		tuiHuoJLManager.save(tuiHuoJL);
		
		//待采买人退货
		toDoManager.deleteList(shenQing);
		ToDo toDo = new ToDo();
		toDo.setCreateDate(new Date());
		toDo.setHuanJie(huanJieManager.get(302));
		toDo.setShenQing(shenQing);
		toDo.setTask("采购退货");
		toDo.setType("申请采购");
		toDo.setUser(shenQing.getCaiMaiR());
		toDoManager.save(toDo);
		Email email=emailManager.getEmail();
		//\\发送邮件
		YouJian youJian=youJianManager.get(302);
		String root=rootManager.getRoot();
		if(youJian!=null && youJian.getState()==1){
			SendMailThread st=new SendMailThread(email, root, 302, wuZi.getCaiMaiR(), youJian);
			st.setWuZi(wuZi);
			st.setTuiHuo(tuiHuo);
			Thread t=new Thread(st);
			t.start();
		}
		response.setContentType("application/json");
		String result="{\"result\":\"success\"}";
		try {
			response.getWriter().write(result);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	@RequestMapping(value = "/cgthPLCX")
	public void cgthPLCX(HttpServletRequest request,HttpServletResponse response){
		String[] type = request.getParameterValues("thId");
		String result="{\"result\":\"none\"}";
		if(type!=null){
			result="{\"result\":\"success\"}";
		}
		response.setContentType("application/json");
		try {
			response.getWriter().write(result);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	@RequestMapping(value = "/cgthPLBTY")
	public ModelAndView cgthPLBTY(HttpServletRequest request){
		String[] type = request.getParameterValues("thId");
		String thIds="";
		for(int i=0;i<type.length;i++){
			thIds=thIds+type[i]+",";
		}
		ModelAndView mav=new ModelAndView();
		mav.addObject("thIds", thIds);
		mav.setViewName("shenqing/cgthPLBTY");
		return mav;
	}
	
	@RequestMapping(value = "/cgthPLBTYSave")
	public void cgthPLBTYSave(String thIds,String boHuiYY,HttpServletResponse response){
		Subject currentUser = SecurityUtils.getSubject();
		Object obj=currentUser.getPrincipals();
		String name=obj.toString();
		User user=userManager.findByUsername(name);
		Email email=emailManager.getEmail();
		String root=rootManager.getRoot();
		String thId[]=thIds.split(",");
		for(int i=0;i<thId.length;i++){
			TuiHuo tuiHuo=tuiHuoManager.get(thId[i]);
			ShenQing shenQing=tuiHuo.getShenQing();
			shenQing.setHuanJie(huanJieManager.get(106));
			shenQingManager.cover(shenQing);
			ShenQingJL shenQingJL=new ShenQingJL();
			shenQingJL.setShenQing(shenQing);
			shenQingJL.setCaoZuoRQ(new Date());
			shenQingJL.setHuanJie(huanJieManager.get(302));		
			shenQingJL.setPiFuLX("采买人不同意退货");
			shenQingJL.setPiFuNR(boHuiYY);
			shenQingJL.setUser(user);
			shenQingJLManager.save(shenQingJL);
			tuiHuo.setHuanJie(huanJieManager.get(304));
			tuiHuoManager.cover(tuiHuo);
			TuiHuoJL tuiHuoJL=new TuiHuoJL();
			tuiHuoJL.setCaoZuoRQ(new Date());
			tuiHuoJL.setHuanJie(huanJieManager.get(302));
			tuiHuoJL.setPiFuLX("采买人不同意退货");
			tuiHuoJL.setPiFuNR(boHuiYY);
			tuiHuoJL.setTuiHuo(tuiHuo);
			tuiHuoJL.setUser(user);
			tuiHuoJLManager.save(tuiHuoJL);
			toDoManager.deleteList(shenQing);
			for(User user1:userManager.getGZGLY()){
				ToDo toDo = new ToDo();
				toDo.setCreateDate(new Date());
				toDo.setHuanJie(huanJieManager.get(106));
				toDo.setShenQing(shenQing);
				toDo.setTask("物资发放");
				toDo.setType("申请采购");
				toDo.setUser(user1);
				toDoManager.save(toDo);
				//\\发送邮件
				YouJian youJian=youJianManager.get(106);
				if(youJian!=null && youJian.getState()==1){
					SendMailThread st=new SendMailThread(email, root, 106, user1, youJian);
					st.setWuZi(shenQing.getWuZi());
					Thread t=new Thread(st);
					t.start();
				}
			}
		}
		String result="{\"result\":\"success\"}";
		response.setContentType("application/json");
		try {
			response.getWriter().write(result);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	@RequestMapping(value = "/cgthBTY")
	public ModelAndView cgthBTY(String id){
		ModelAndView mav=new ModelAndView();
		mav.addObject("thId", id);
		mav.setViewName("shenqing/cgthBTY");
		return mav;
	}
	
	@RequestMapping(value = "/cgthBTYSave")
	public void cgthBTYSave(String thId,String boHuiYY,HttpServletResponse response){
		Subject currentUser = SecurityUtils.getSubject();
		Object obj=currentUser.getPrincipals();
		String name=obj.toString();
		User user=userManager.findByUsername(name);
		TuiHuo tuiHuo=tuiHuoManager.get(thId);
		ShenQing shenQing=tuiHuo.getShenQing();
		shenQing.setHuanJie(huanJieManager.get(106));
		shenQingManager.cover(shenQing);
		ShenQingJL shenQingJL=new ShenQingJL();
		shenQingJL.setShenQing(shenQing);
		shenQingJL.setCaoZuoRQ(new Date());
		shenQingJL.setHuanJie(huanJieManager.get(302));		
		shenQingJL.setPiFuLX("采买人不同意退货");
		shenQingJL.setPiFuNR(boHuiYY);
		shenQingJL.setUser(user);
		shenQingJLManager.save(shenQingJL);
		tuiHuo.setHuanJie(huanJieManager.get(304));
		tuiHuoManager.cover(tuiHuo);
		TuiHuoJL tuiHuoJL=new TuiHuoJL();
		tuiHuoJL.setCaoZuoRQ(new Date());
		tuiHuoJL.setHuanJie(huanJieManager.get(302));
		tuiHuoJL.setPiFuLX("采买人不同意退货");
		tuiHuoJL.setPiFuNR(boHuiYY);
		tuiHuoJL.setTuiHuo(tuiHuo);
		tuiHuoJL.setUser(user);
		tuiHuoJLManager.save(tuiHuoJL);
		toDoManager.deleteList(shenQing);
		Email email=emailManager.getEmail();
		String root=rootManager.getRoot();
		for(User user1:userManager.getGZGLY()){
			ToDo toDo = new ToDo();
			toDo.setCreateDate(new Date());
			toDo.setHuanJie(huanJieManager.get(106));
			toDo.setShenQing(shenQing);
			toDo.setTask("物资发放");
			toDo.setType("申请采购");
			toDo.setUser(user1);
			toDoManager.save(toDo);
			//\\发送邮件
			YouJian youJian=youJianManager.get(106);
			if(youJian!=null && youJian.getState()==1){
				SendMailThread st=new SendMailThread(email, root, 106, user1, youJian);
				st.setWuZi(shenQing.getWuZi());
				Thread t=new Thread(st);
				t.start();
			}
		}
		String result="{\"result\":\"success\"}";
		response.setContentType("application/json");
		try {
			response.getWriter().write(result);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	@RequestMapping(value = "/thqrList")
	public ModelAndView thqrList(){
		ModelAndView mav=new ModelAndView();
		mav.addObject("tuiHuoQRList", tuiHuoManager.getAllTuiHuoSQ(303));
		mav.setViewName("shenqing/thqrListAjax");
		return mav;
	}
	
	@RequestMapping(value = "/tuiHuoQR")
	public void tuiHuoQR(String id,HttpServletResponse response){
		Subject currentUser = SecurityUtils.getSubject();
		Object obj=currentUser.getPrincipals();
		String name=obj.toString();
		User user=userManager.findByUsername(name);
		TuiHuo tuiHuo=tuiHuoManager.get(id);
		tuiHuo.setHuanJie(huanJieManager.get(304));
		tuiHuoManager.cover(tuiHuo);
		TuiHuoJL tuiHuoJL=new TuiHuoJL();
		tuiHuoJL.setCaoZuoRQ(new Date());
		tuiHuoJL.setHuanJie(huanJieManager.get(303));
		tuiHuoJL.setPiFuLX("固资管理员确认");
		tuiHuoJL.setPiFuNR("同意");
		tuiHuoJL.setTuiHuo(tuiHuo);
		tuiHuoJL.setUser(user);
		tuiHuoJLManager.save(tuiHuoJL);
		ShenQing shenQing=tuiHuo.getShenQing();
		shenQing.setHuanJie(huanJieManager.get(304));
		shenQingManager.cover(shenQing);
		ShenQingJL shenQingJL=new ShenQingJL();
		shenQingJL.setCaoZuoRQ(new Date());
		shenQingJL.setHuanJie(huanJieManager.get(303));
		shenQingJL.setPiFuLX("固资管理员确认");
		shenQingJL.setPiFuNR("同意");
		shenQingJL.setShenQing(shenQing);
		shenQingJL.setUser(user);
		shenQingJLManager.save(shenQingJL);		
		
		toDoManager.deleteList(shenQing);
		response.setContentType("application/json");
		String result="{\"result\":\"success\"}";
		try {
			response.getWriter().write(result);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	@RequestMapping(value = "/thqrPL")
	public void tuiHuoQR(HttpServletRequest request,HttpServletResponse response){
		Subject currentUser = SecurityUtils.getSubject();
		Object obj=currentUser.getPrincipals();
		String name=obj.toString();
		User user=userManager.findByUsername(name);
		String type[]=request.getParameterValues("thqrId");
		if(type!=null){
			for(int i=0;i<type.length;i++){
				TuiHuo tuiHuo=tuiHuoManager.get(type[i]);
				tuiHuo.setHuanJie(huanJieManager.get(304));
				tuiHuoManager.cover(tuiHuo);
				TuiHuoJL tuiHuoJL=new TuiHuoJL();
				tuiHuoJL.setCaoZuoRQ(new Date());
				tuiHuoJL.setHuanJie(huanJieManager.get(303));
				tuiHuoJL.setPiFuLX("固资管理员确认");
				tuiHuoJL.setPiFuNR("同意");
				tuiHuoJL.setTuiHuo(tuiHuo);
				tuiHuoJL.setUser(user);
				tuiHuoJLManager.save(tuiHuoJL);
				ShenQing shenQing=tuiHuo.getShenQing();
				shenQing.setHuanJie(huanJieManager.get(304));
				shenQingManager.cover(shenQing);
				ShenQingJL shenQingJL=new ShenQingJL();
				shenQingJL.setCaoZuoRQ(new Date());
				shenQingJL.setHuanJie(huanJieManager.get(303));
				shenQingJL.setPiFuLX("固资管理员确认");
				shenQingJL.setPiFuNR("同意");
				shenQingJL.setShenQing(shenQing);
				shenQingJL.setUser(user);
				shenQingJLManager.save(shenQingJL);		
				
				toDoManager.deleteList(shenQing);
			}
		}
		response.setContentType("application/json");
		String result="{\"result\":\"success\"}";
		try {
			response.getWriter().write(result);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	@RequestMapping(value = "/caiGouTuiHuoTY")
	public void caiGouTuiHuoTY(String id,HttpServletResponse response){
		Subject currentUser = SecurityUtils.getSubject();
		Object obj=currentUser.getPrincipals();
		String name=obj.toString();
		User user=userManager.findByUsername(name);
		TuiHuo tuiHuo=tuiHuoManager.get(id);
		ShenQing shenQing=tuiHuo.getShenQing();
		WuZi wuZi=tuiHuo.getWuZi();		
		shenQing.setHuanJie(huanJieManager.get(303));		
		shenQingManager.cover(shenQing);
		ShenQingJL shenQingJL=new ShenQingJL();
		shenQingJL.setShenQing(shenQing);
		shenQingJL.setCaoZuoRQ(new Date());
		shenQingJL.setHuanJie(huanJieManager.get(302));		
		shenQingJL.setPiFuLX("采买人同意退货");
		shenQingJL.setPiFuNR("同意");
		shenQingJL.setUser(user);
		shenQingJLManager.save(shenQingJL);
		tuiHuo.setHuanJie(huanJieManager.get(303));
		tuiHuoManager.cover(tuiHuo);
		TuiHuoJL tuiHuoJL=new TuiHuoJL();
		tuiHuoJL.setCaoZuoRQ(new Date());
		tuiHuoJL.setTuiHuo(tuiHuo);
		tuiHuoJL.setHuanJie(huanJieManager.get(302));
		tuiHuoJL.setPiFuLX("采买人同意退货");
		tuiHuoJL.setPiFuNR("同意");
		tuiHuoJL.setUser(user);
		tuiHuoJLManager.save(tuiHuoJL);
		wuZi.setShiYongZT(shiYongZTManager.get(104));
		wuZiManager.cover(wuZi);
		Email email=emailManager.getEmail();
		String root=rootManager.getRoot();
		toDoManager.deleteList(shenQing);
		for(User user1:userManager.getGZGLY()){
			ToDo toDo = new ToDo();
			toDo.setCreateDate(new Date());
			toDo.setHuanJie(huanJieManager.get(303));
			toDo.setShenQing(shenQing);
			toDo.setTask("退货确认");
			toDo.setType("申请采购");
			toDo.setUser(user1);
			toDoManager.save(toDo);
			//\\发送邮件
			YouJian youJian=youJianManager.get(303);
			if(youJian!=null && youJian.getState()==1){
				SendMailThread st=new SendMailThread(email, root, 303, user1, youJian);
				st.setWuZi(wuZi);
				st.setTuiHuo(tuiHuo);
				Thread t=new Thread(st);
				t.start();
			}
		}
		response.setContentType("application/json");
		String result="{\"result\":\"success\"}";
		try {
			response.getWriter().write(result);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	@RequestMapping(value = "/cgthPL")
	public void cgthPL(HttpServletRequest request,HttpServletResponse response){
		Subject currentUser = SecurityUtils.getSubject();
		Object obj=currentUser.getPrincipals();
		String name=obj.toString();
		User user=userManager.findByUsername(name);
		Email email=emailManager.getEmail();
		String root=rootManager.getRoot();
		String type[]=request.getParameterValues("thId");
		if(type!=null){
			for(int i=0;i<type.length;i++){
				TuiHuo tuiHuo=tuiHuoManager.get(type[i]);
				ShenQing shenQing=tuiHuo.getShenQing();
				WuZi wuZi=tuiHuo.getWuZi();		
				shenQing.setHuanJie(huanJieManager.get(303));		
				shenQingManager.cover(shenQing);
				ShenQingJL shenQingJL=new ShenQingJL();
				shenQingJL.setShenQing(shenQing);
				shenQingJL.setCaoZuoRQ(new Date());
				shenQingJL.setHuanJie(huanJieManager.get(302));		
				shenQingJL.setPiFuLX("采买人同意退货");
				shenQingJL.setPiFuNR("同意");
				shenQingJL.setUser(user);
				shenQingJLManager.save(shenQingJL);
				tuiHuo.setHuanJie(huanJieManager.get(303));
				tuiHuoManager.cover(tuiHuo);
				TuiHuoJL tuiHuoJL=new TuiHuoJL();
				tuiHuoJL.setCaoZuoRQ(new Date());
				tuiHuoJL.setHuanJie(huanJieManager.get(302));
				tuiHuoJL.setPiFuLX("采买人同意退货");
				tuiHuoJL.setPiFuNR("同意");
				tuiHuoJL.setTuiHuo(tuiHuo);
				tuiHuoJL.setUser(user);
				tuiHuoJLManager.save(tuiHuoJL);
				wuZi.setShiYongZT(shiYongZTManager.get(104));
				wuZiManager.cover(wuZi);
				
				toDoManager.deleteList(shenQing);
				for(User user1:userManager.getGZGLY()){
					ToDo toDo = new ToDo();
					toDo.setCreateDate(new Date());
					toDo.setHuanJie(huanJieManager.get(303));
					toDo.setShenQing(shenQing);
					toDo.setTask("退货确认");
					toDo.setType("申请采购");
					toDo.setUser(user1);
					toDoManager.save(toDo);
					//\\发送邮件
					YouJian youJian=youJianManager.get(303);
					if(youJian!=null && youJian.getState()==1){
						SendMailThread st=new SendMailThread(email, root, 303, user1, youJian);
						st.setWuZi(wuZi);
						st.setTuiHuo(tuiHuo);
						Thread t=new Thread(st);
						t.start();
					}
				}
			}
		}
		response.setContentType("application/json");
		String result="{\"result\":\"success\"}";
		try {
			response.getWriter().write(result);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
}
